package com.hongxun.budget.ui.activity.tk;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.greendao.BDeviceEntityDao;
import com.hongxun.budget.ui.activity.BaseListActivity;
import com.hongxun.budget.utils.GreenDaoManager;
import com.hongxun.greenentity.tk.BDeviceEntity;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.view.ViewHolder;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SearchDeviceActivity extends BaseListActivity {
    @BindView(R.id.search_info)
    EditText searchInfo;

    List<BDeviceEntity> deviceEntities = new ArrayList<>();
    SuperAdapter<BDeviceEntity> rightAdapter;

    @Override
    public int getMyListLayoutId() {
        return R.layout.activity_search_device;
    }

    @Override
    public SuperAdapter getAdapter() {
        return rightAdapter = new SuperAdapter<BDeviceEntity>(R.layout.content_add_device) {
            @Override
            public void bindView(ViewHolder viewHolder, final BDeviceEntity bDeviceEntity, int i) {
                ((TextView) viewHolder.getView(R.id.name)).setText(bDeviceEntity.getDName());
                ((TextView) viewHolder.getView(R.id.type)).setText(bDeviceEntity.getDLabel());
                ((TextView) viewHolder.getView(R.id.parameter)).setText(bDeviceEntity.getDParameter());
                viewHolder.getView(R.id.layout).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent();
                        intent.putExtra("device", bDeviceEntity);
                        setResult(Activity.RESULT_OK, intent);
                        finish();
                    }
                });
            }
        };
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        rightAdapter.setData(deviceEntities);
        rightAdapter.setEmptyDataView(R.layout.view_empty);
    }

    @OnClick(R.id.search_button)
    public void onViewClicked() {
        setRightList();
    }

    void setRightList() {
        String search = searchInfo.getText().toString();
        if (TextUtils.isEmpty(search)) {
            showMsg("请输入您要关键字查询");
            return;
        }
        QueryBuilder<BDeviceEntity> builder = GreenDaoManager.getInstance().getmDaoSession().getBDeviceEntityDao().queryBuilder();
        List<BDeviceEntity> entities = builder.where(builder.or(BDeviceEntityDao.Properties.DName.like("%" + search + "%")
                , BDeviceEntityDao.Properties.DBParameter.like("%" + search + "%"))).list();
        deviceEntities.clear();
        deviceEntities.addAll(entities);
        rightAdapter.notifyDataSetChanged();
    }
}
